/**
 * Filename: AlbumMemberAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/8 12:11
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.bean.YNChart;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

import java.util.List;


public class HuoDongChartAdapter extends RecyclerView.Adapter<HuoDongChartAdapter.ViewHolder> {
    private Context mContext;
    private List<YNChart> datas;
    private ImageLoader imageLoader;

    public HuoDongChartAdapter(Context mContext, List<YNChart> datas, YNAlbum mGroup, boolean remove, String curID) {
        this.mContext = mContext;
        this.datas = datas;
        imageLoader = ImageLoader.getInstance();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_item_album_member;                                //列表View
        TextView rl_item_album_point;
        ImageView iv_item_album_member_avatar;                        //成员头像
        ImageView paihang_top;
        TextView tv_item_album_member_username;                        //成员名
        TextView rl_item_album_num;

        public ViewHolder(View itemView) {
            super(itemView);
            ll_item_album_member = (LinearLayout) itemView
                    .findViewById(R.id.ll_item_album_member);
            rl_item_album_point = (TextView) itemView
                    .findViewById(R.id.rl_item_album_point);
            iv_item_album_member_avatar = (ImageView) itemView
                    .findViewById(R.id.iv_item_album_member_avatar);
            paihang_top = (ImageView) itemView
                    .findViewById(R.id.paihang_top);
            tv_item_album_member_username = (TextView) itemView
                    .findViewById(R.id.tv_item_album_member_username);
            rl_item_album_num = (TextView) itemView
                    .findViewById(R.id.rl_item_album_num);
        }

        public void bindData(final YNChart listItems, final int point) {
            if (listItems != null) {
                rl_item_album_point.setText(listItems.getRank() + ".");
                imageLoader.displayImage(listItems.getUpic() + StringUtils.getQulity(),
                        new ImageViewAware(iv_item_album_member_avatar, false),
                        ImageOptHelper.getAvatarOptions());
                tv_item_album_member_username.setText(listItems.getUnickname());
                rl_item_album_num.setText(String.valueOf(listItems.getNum()));
                if (listItems.getRank().equals("1")) {
                    paihang_top.setImageResource(R.drawable.paihang_jin);
                    paihang_top.setVisibility(View.VISIBLE);
                    rl_item_album_num.setTextColor(Color.parseColor("#333333"));
                    rl_item_album_num.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                } else if (listItems.getRank().equals("2")) {
                    paihang_top.setImageResource(R.drawable.paihang_yin);
                    paihang_top.setVisibility(View.VISIBLE);
                    rl_item_album_num.setTextColor(Color.parseColor("#666666"));
                    rl_item_album_num.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                } else if (listItems.getRank().equals("3")) {
                    paihang_top.setImageResource(R.drawable.paihang_tong);
                    paihang_top.setVisibility(View.VISIBLE);
                    rl_item_album_num.setTextColor(Color.parseColor("#666666"));
                    rl_item_album_num.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                } else {
                    paihang_top.setVisibility(View.GONE);
                }
            }
        }
    }

    private YNChart getItem(int position) {
        return datas.get(position);
    }


    // 创建ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_huodong_chart, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        YNChart listItems = getItem(position);
        holder.bindData(listItems, position);
        holder.itemView.setTag(listItems);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }
}
